package core.sceneManager.sceneSection.scene2DSection
{
	import core.MainManager;
	import core.sceneManager.sceneSection.AnimateSection;
	import core.sceneManager.sceneSection.IMouseBehavior;
	import core.sceneManager.sceneSection.ITipBehavior;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import memory.MemoryRecovery;
	
	import utils.XmlUtil;
	
	import view.ToolTip;
	/**
	 * 2D场景中的图片或SWF
	 */ 
	public class SwfOrBitmapSection extends AnimateSection implements IMouseBehavior, ITipBehavior
	{
		private var _mouseClickScript:String;     //鼠标点击时所要执行的脚本
		private var loader:Loader;                //用来加载数据
		private var loaderContainer:Sprite;       //loader的容器。让loader拥有鼠标手型
		private var numTries:int=0;               //数据加载失败的时候，重试的次数
		private const maxTries:int=3;             //数据加载失败的时候，最大的重试次数
		private var request:URLRequest;
		private var context:LoaderContext;
		private var _englishTip:String;           //英文提示
		private var _chineseTip:String;           //中文提示
		private var waitForComplete:int;
		private var url:String;
		public function SwfOrBitmapSection(target:IEventDispatcher=null)
		{
			super(target);
			loader=new Loader();
			loaderContainer=new Sprite();
			loaderContainer.addChild(loader);
		}
		override public function excute(xml:XML, container:*):void
		{
			this.dataBase=xml;
			for each(var item:XML in xml.attributes())
			{
				var key:String=item.name();
				try
				{
					this[key]=item.toString();
				}catch(e:Error)
				{
				}
			}
			container.addChild(loaderContainer);
		}
		override public function start():void
		{
			request=new URLRequest(url);
			loader.load(request,context);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,on_loaded);
		}
		override public function pause():void
		{
			try
			{
				loader.content["pause"]();
			}catch(e:Error){}
		}
		override public function resume():void
		{
			try
			{
				loader.content["resume"]();
			}catch(e:Error){}
		}
		override public function play():void
		{
			try
			{
				loader.content["play"]();
			}catch(e:Error){}
		}
		//SWF或图片加载完毕
		private function on_loaded(e:Event):void
		{
			MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,Event.COMPLETE,on_loaded);
			if(dataBase.Content!=null)
			{
			for each(var item:XML in dataBase.Content.attributes())
			{
				var key:String=item.name();
				try
				{
					loader.content[key]=item.toString();
				}catch(e:Error)
				{
				}
			}
			}
			if(this.waitForComplete==1) this.dispatchEvent(e);
		}
		/**
		 * 回收内存
		 */ 
		override public function dispose():void
		{
			super.dispose();
			MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,Event.COMPLETE,on_loaded);
			MemoryRecovery.getInstance().gcFun(loader,MouseEvent.CLICK,on_click);
			_mouseClickScript=null;
			request=null;
			context=null;
			_englishTip=null;
			_chineseTip=null;
			MemoryRecovery.getInstance().gcObj(loader);
			loader=null;
		}
		//////////////////////////////////////////声明属性，可以通过XML配置/////////////////////////
		//设置在界面中的层次,默认是1层，也就是最底层
		private function set layer(val:int):void
		{
			if(MainManager.getInstance().getContainer(val)!=loader.parent)
			{
				MainManager.getInstance().getContainer(val).addChild(loader);
			}
		}
		//SWF的点击事件
		public function set mouseClickScript(script:String):void
		{
			this._mouseClickScript=script;
			loader.addEventListener(MouseEvent.CLICK,on_click);
			loaderContainer.buttonMode=true;
			loaderContainer.useHandCursor=true;
		}
		private function on_click(e:MouseEvent):void
		{
			MainManager.getInstance().scriptManager.runScriptDirectly(this._mouseClickScript);
		}
		private function set domain(val:String):void
		{
			context=new LoaderContext();
			if(val=="" || val=="new")
			{
				/* 加载到新域(独立运行的程序或模块) */
			}
			if(val=="current")
			{
				/* 加载到同域(共享库) */
				context.applicationDomain = ApplicationDomain.currentDomain;
			}
			if(val=="child")
			{
				/* 加载到子域(模块) */
				context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
			}
		}
		//设置英文tip
		public function set englishTip(val:String):void
		{
			this._englishTip=val;
		}
		public function set chineseTip(val:String):void
		{
			this._chineseTip=val;
			MainManager.getInstance().handleLanguageChange(showChineseTip,showEnglishTip);
		}
		private function showChineseTip():void
		{
			ToolTip.unregister(loader);
			ToolTip.register(loader,this._chineseTip);
		}
		private function showEnglishTip():void
		{
			ToolTip.unregister(loader);
			ToolTip.register(loader,this._englishTip);
		}
		private function set x(val:Number):void{loader.x=val;}
		private function set y(val:Number):void{loader.y=val;}
		private function set scaleX(val:Number):void{loader.scaleX=val;}
		private function set scaleY(val:Number):void{loader.scaleY=val;}
		private function set rotation(val:Number):void{loader.rotation=val;}
	}
}